package com.ss.whisper2.rabbitmq.topic;

import com.ss.whisper2.exception.GlobalException;
import com.ss.whisper2.pojo.Result;
import com.ss.whisper2.pojo.State;
import com.ss.whisper2.pojo.entity.course.SelectCourseMessage;
import com.ss.whisper2.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


@Service
public class TopicReceiver {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	CourseService courseService;

	@RabbitListener(queues = "topic.course.select")
	public void receiveTopicCourseSelect(SelectCourseMessage message) {
		logger.info("消费者course.select收到消息:" + message.toString());
		String username =message.getUsername();
		Integer courseId=message.getCourseId();
		if (courseService.getSelectedCourseResult(username, courseId))
			return;
			//throw new GlobalException(State.DUPLICATE_SELECT);
		if (courseService.getCourseRest(courseId)<=0)
			return;
			//throw new GlobalException(State.NO_REST);
		try {
			courseService.selectCourse(username, courseId);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

	}
}